<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模拟框拖拽</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .open{
            width: 100%;
            height: 50px;
            /* z-index: 10px; */
            line-height: 50px;
            text-align: center;
        }
        .login{
            position: absolute;
            top: 50%;
            left: 50%;
            /* 偏右下，再移动自己宽、高的一半 */
            transform: translate(-50%,-50%);
            display: none;
            width: 500px;
            height: 200px;
            z-index: 10;
            background-color: #fff;
            border: 1px solid #ccc;
        }
        .login-title{
            width: 100%;
            height: 60px;
            line-height: 60px;
            text-align: center;
            font-size: 18px;
            background-color: #eee;
            border-bottom: 1px solid #ccc;
        }
        .close{
            position: absolute;
            top: -40px;
            right: -40px;
            width: 80px;
            height: 80px;
            /* border: 1px solid #ccc; */
            border-radius: 40px;
            background-color: #eee;
        }
        .mask{
            position: fixed;
            width:100%;
            height: 100%;
            top: 0;
            left: 0;
            display: none;
            z-index: 8;
            background-color: rgba(0, 0, 0, 0.3);
        }
    </style>
</head>
<body>
    <div class="open">点击打开模态框</div>
    <!-- 模态框 -->
    <div class="login">
        <div class="login-title">
            会员登录
            <div class="close">关闭</div>
        </div>
    </div>
    <!-- <div class="close">关闭</div> -->
    <!-- 遮挡层 -->
    <div class="mask"></div>

    <script>
        var open=document.querySelector('.open');
        var close=document.querySelector('.close');
        var login=document.querySelector('.login');
        var mask=document.querySelector('.mask');
        var title=document.querySelector('.login-title');

        //单击打开按钮，显示模态框和遮挡层
        open.addEventListener('click',function(){
            login.style.display='block';
            mask.style.display='block';
        })
        //单击关闭按钮，隐藏模态框和遮挡层
        close.addEventListener('click',function(){
            login.style.display='none';
            mask.style.display='none';
        })
        title.addEventListener('mousedown',function(e){
            //计算出鼠标在模态框中的x、y坐标值
            //此坐标在拖拽过程中是不变的
            mx=e.pageX-login.offsetLeft;
            my=e.pageY-login.offsetTop;

            title.addEventListener('mousemove',move);

            function move(e){
            //计算并赋值模态框x、y坐标值
                var x=e.pageX-mx;
                var y=e.pageY-my;
                login.style.left=x+'px';
                login.style.top=y+'px';
            }
        
            title.addEventListener('mouseup',function(){
                title.removeEventListener('mousemove',move);
            });
        });
       
    </script>
</body>
</html>